﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxUploadControl;
using System.Collections;
using DevExpress.Web.ASPxEditors;

public partial class quanlichuongtrinh : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.DataSource = ChuongTrinh.GetLoaiCT();
            DropDownList1.DataTextField = "tenloaict";
            DropDownList1.DataValueField = "maloaict";
            DropDownList1.SelectedIndex = 0;
            DropDownList1.DataBind();
        }
        load();
    }
    
    private void load()
    {
        gv_qlChuongTrinh.DataSource = ChuongTrinh.GetAll();
        gv_qlChuongTrinh.DataBind();
    }
    protected void gv_qlChuongTrinh_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
    {
        int mact = int.Parse(e.Values["ct.mact"].ToString());//de control chay duoc cac su kien thi can co cot id(ma cua doi tuong can thao tac)
        ChuongTrinh.DeleteChuongtrinh(mact);
        e.Cancel = true;
        gv_qlChuongTrinh.CancelEdit();
        load();
    }
    protected void gv_qlChuongTrinh_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {
        ASPxLabel mact = (ASPxLabel)gv_qlChuongTrinh.FindEditFormTemplateControl("ASPxLabel1");
        ASPxTextBox tenct = (ASPxTextBox)gv_qlChuongTrinh.FindEditFormTemplateControl("ASPxTextBox1");
        ASPxTextBox mota = (ASPxTextBox)gv_qlChuongTrinh.FindEditFormTemplateControl("ASPxTextBox4");
        ASPxTextBox thoiluong = (ASPxTextBox)gv_qlChuongTrinh.FindEditFormTemplateControl("ASPxTextBox3");

        FileUpload hinh = (FileUpload)gv_qlChuongTrinh.FindEditFormTemplateControl("FU_taihinh");
        ASPxLabel imageFile = (ASPxLabel)gv_qlChuongTrinh.FindEditFormTemplateControl("ASPxLabel2");
        ChuongTrinh.UpdateChuongTrinh(int.Parse(mact.Text), tenct.Text, int.Parse(thoiluong.Text), mota.Text, imageFile.Text);
        e.Cancel = true;
        gv_qlChuongTrinh.CancelEdit();
        load();
    }
    protected void gv_qlChuongTrinh_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string image = "";
        string tenct = txt_TenCt.Text;
        string maloaiCt = DropDownList1.SelectedItem.Value;
        string thoiluong = txt_ThoiLuong.Text;
        string mota = txt_Mota.Text;
        if(FileUpload1.HasFile)
        {
            FileUpload1.PostedFile.SaveAs(Server.MapPath("../images_chuongtrinh/")+FileUpload1.FileName);
            image = FileUpload1.FileName;
        }
        ChuongTrinh.ThemChuongTrinh(tenct, Convert.ToInt32(thoiluong), mota, Convert.ToInt32(maloaiCt), image);
        load();
    }
}